package com.yangguang.huahui.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.text.Spanned;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yangguang.huahui.widget.ProgressDialog;
import com.yangguang.huahui.widget.SpinView;


/**
 * 对话框工具类
 * 
 */
public final class DialogUtil {

	/**
	 * 只有确定按钮的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showOKDialog(Context context, String title,
			String message, OnClickListener listener) {
		try {
			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle(title);
			dialog.setMessage(message);
			dialog.setPositiveButton("确定", listener);
			dialog.setCancelable(false);
			dialog.show();
			return dialog.create();
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return null;
	}


	/**
	 * 只有确定按钮的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showOKDialog(Context context, int title, int message,
			OnClickListener listener) {
		return showOKDialog(context, context.getString(title),
				context.getString(message), listener);
	}

	/**
	 * 有两个按钮的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param btnStr1
	 * @param btnStr2
	 * @param listener
	 * @return
	 */
	public static Dialog showTowButtonDialog(Context context, String title,
			Spanned message, String btnStr1, String btnStr2,
			OnClickListener listener) {
		try {
			AlertDialog dialog = new AlertDialog.Builder(context).create();
			if (!CommonUtil.isNull(title)) {
				dialog.setTitle(title);
			}
			dialog.setMessage(message);
			dialog.setButton(0, btnStr1, listener);
			dialog.setButton(1, btnStr2, listener);
			dialog.show();
			return dialog;
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return null;
	}

	/**
	 * 有两个按钮的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param btnStr1
	 * @param btnStr2
	 * @param listener
	 * @return
	 */
	public static Dialog showTowButtonDialog(Context context, String title,
			String message, String btnStr1, String btnStr2,
			OnClickListener listener) {
		try {
			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			dialog.setTitle(title);
			dialog.setMessage(message);
			dialog.setPositiveButton(btnStr2, listener);
			dialog.setNegativeButton(btnStr1, listener);
			dialog.show();
			return dialog.create();
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return null;
	}

	/**
	 * 有两个按钮的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param btnStr1
	 * @param btnStr2
	 * @param listener
	 * @return
	 */
	public static Dialog showTowButtonDialog(Context context, int title,
			int message, int btnStr1, int btnStr2, OnClickListener listener) {
		return showTowButtonDialog(context, context.getString(title),
				context.getString(message), context.getString(btnStr1),
				context.getString(btnStr2), listener);
	}

	/**
	 * 有一个按钮的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showOneButtonDialog(Context context, String title,
			String message, String btnStr, OnClickListener listener) {
		try {
			AlertDialog dialog = new AlertDialog.Builder(context).create();
			dialog.setTitle(title);
			dialog.setMessage(message);
			dialog.setButton(0, btnStr, listener);
			dialog.show();
			return dialog;
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return null;
	}

	public static Dialog showTwoButtonDialog(Context context, String title,
			String message, String button1, String button2,
			OnClickListener listener) {
		try {
			AlertDialog.Builder dialog = new AlertDialog.Builder(context);
			if (!CommonUtil.isNull(title)){
				dialog.setTitle(title);
			}
			dialog.setMessage(message);
			dialog.setPositiveButton(button1, listener);
			dialog.setNegativeButton(button2, listener);
			dialog.show();
			return dialog.create();
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return null;
	}

	/**
	 * 有确定和取消的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showOKCancelDialog(Context context, String title,
			String message, OnClickListener listener) {
		return showTwoButtonDialog(context, title, message,
				"确认",
				"取消", listener);
	}

	/**
	 * 有确定和取消的对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showOKCancelDialog(Context context, int title,
			int message, OnClickListener listener) {
		return showOKCancelDialog(context, context.getString(title),
				context.getString(message), listener);
	}

	/**
	 * 滚动条对话框
	 * 
	 * @param context
	 * @param message
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Dialog showProgressDialog(Context context,
			String message) {
		try {
			ProgressDialog dialog = new ProgressDialog(context);
//			dialog.setView(new SpinView(context));
			dialog.setLabel(message);
			dialog.show();
			return dialog;
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return null;
	}

	/**
	 * 滚动条对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showProgressDialog(Context context, int title,
			int message, OnCancelListener listener) {
		return showProgressDialog(context,
				context.getString(message));
	}

	/**
	 * 错误消息对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showErrorDialog(Context context, String title,
			String message, OnClickListener listener) {
		AlertDialog dialog = (AlertDialog) showOKDialog(context, title,
				message, listener);
		if (dialog != null) {
			dialog.setIcon(android.R.drawable.ic_dialog_alert);
		}
		return dialog;
	}

	/**
	 * 错误消息对话框
	 * 
	 * @param context
	 * @param title
	 * @param message
	 * @param listener
	 * @return
	 */
	public static Dialog showErrorDialog(Context context, int title,
			int message, OnClickListener listener) {
		return showErrorDialog(context, context.getString(title),
				context.getString(message), listener);
	}

	/**
	 * 自定义内容的对话框
	 * 
	 * @param context
	 * @param title
	 * @param content
	 * @return
	 */
	public static Dialog showCustomDialog(Context context, String title,
			View content) {
		try {
			AlertDialog dialog = new AlertDialog.Builder(context).create();
			dialog.setTitle(title);
			dialog.setView(content);
			dialog.show();
			return dialog;
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return null;
	}

	/**
	 * 自定义内容的对话框
	 * 
	 * @param context
	 * @param title
	 * @param content
	 * @return
	 */
	public static Dialog showCustomDialog(Context context, int title,
			View content) {
		return showCustomDialog(context, context.getString(title), content);
	}
}
